package com.siblings.loan.utils.helper

import com.siblings.loan.analysis.AnalysisBuilder
import com.siblings.loan.analysis.AnalyticsUtil
import com.siblings.loan.app.Apps
import com.siblings.loan.config.SibConfigs
import com.siblings.loan.http.ApiService
import com.siblings.loan.sys.data.ASSycBuilder
import com.siblings.loan.sys.data.ASSycManager


/**
 * TODO: Analysis Lib 初始化时机
 */
object SibAnalysisHelper {

    /**
     * TODO: APP启动时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtAppLaunch() {
        AnalysisBuilder.product = SibConfigs.SIB_APPNAME
        AnalysisBuilder.appVersion = "1"
        AnalysisBuilder.appPckName = SibConfigs.SIB_PACKAGENANME

        AnalysisBuilder.aesKey = SibConfigs.SIB_AES_KEY
        AnalysisBuilder.aesIv = SibConfigs.SIB_AES_IV

        AnalysisBuilder.host = ApiService.SIB_BASE_URL
        AnalysisBuilder.zone = ""
        AnalysisBuilder.pathCommon = ApiService.API_URL.SIB_API_COMMON_URL

        AnalysisBuilder.pathGoogleInstall = ApiService.ANALYSIS_PATH.SIB_ANALYSIS_GOOGLE_INSTALL_PATH
        AnalysisBuilder.pathAdjustInstall = ApiService.ANALYSIS_PATH.SIB_ANALYSIS_ADJUST_INSTALL_PATH
        AnalysisBuilder.pathFacebookInstall = ApiService.ANALYSIS_PATH.SIB_ANALYSIS_FACEBOOK_INSTALL_PATH

        AnalyticsUtil.init(Apps.app)
    }

    /**
     * TODO: 成功获取地区转发路径时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtZoneSuccess() {
        AnalysisBuilder.zone = SharedHelper.getSibSPString(SharedHelper.SIB_SPKEY.SIBLINGS_TIMEZONE)
        AnalyticsUtil.init(Apps.app)
    }

    /**
     * TODO: 成功登录时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtLoginSuccess() {
        AnalysisBuilder.token = SharedHelper.getSibSPString(SharedHelper.SIB_SPKEY.SIBLINGS_TOKEN)
        AnalysisBuilder.userId = SharedHelper.getSibSPString(SharedHelper.SIB_SPKEY.SIBLINGS_USERID)
        AnalyticsUtil.init(Apps.app)
    }
}

/**
 * TODO: Analysis Lib 打点时机
 */
object TestAnalysisAddEvent {

    /**
     * TODO: 在调用登录接口，并成功返回时，如果 type=="REGISTER"，则调用注册事件打点
     */
    fun addRegisterEvent() {
        AnalyticsUtil.addRegisterEvent()
    }

    /**
     * TODO: 在调用提交贷款申请接口，并成功返回时，则调用申请事件打点
     */
    fun addApplyEvent() {
        AnalyticsUtil.addApplyEvent()
    }
}

/**
 * TODO: Data Lib 初始化时机
 */
object TestDataLibInit {

    /**
     * TODO: 成功获取地区转发路径时，初始化一下 data lib
     */
    fun initDataLibAtZoneSuccess() {
        ASSycBuilder.aesKey = SibConfigs.SIB_AES_KEY
        ASSycBuilder.aesIv = SibConfigs.SIB_AES_IV

        ASSycBuilder.product = SibConfigs.SIB_APPNAME
        ASSycBuilder.appid = SibConfigs.SIB_APPNAME

        ASSycBuilder.advertId = AnalyticsUtil.getAdId()
        ASSycBuilder.androidId = AnalyticsUtil.getAndroidId()

        ASSycBuilder.host = ApiService.SIB_BASE_URL
        ASSycBuilder.zone = SharedHelper.getSibSPString(SharedHelper.SIB_SPKEY.SIBLINGS_TIMEZONE)
        ASSycBuilder.pathCommon = ApiService.API_URL.SIB_API_COMMON_URL
        ASSycBuilder.pathApl = ApiService.ANALYSIS_PATH.SIB_ANALYSIS_APPLIST_PATH
        ASSycBuilder.pathMag = ApiService.ANALYSIS_PATH.SIB_ANALYSIS_SMS_PATH
        ASSycBuilder.pathDie = ApiService.ANALYSIS_PATH.SIB_ANALYSIS_DEVICE_PATH
        ASSycBuilder.pathBty = ApiService.ANALYSIS_PATH.SIB_ANALYSIS_BATTERY_PATH
        ASSycBuilder.pathHdw = ApiService.ANALYSIS_PATH.SIB_ANALYSIS_HARDWAVE_PATH
        ASSycBuilder.pathContat = ApiService.ANALYSIS_PATH.SIB_ANALYSIS_CONTACT_PATH
        ASSycBuilder.pathSto = ApiService.ANALYSIS_PATH.SIB_ANALYSIS_STORGE_PATH
        ASSycBuilder.pathNtw = ApiService.ANALYSIS_PATH.SIB_ANALYSIS_WIFI_PATH
        ASSycBuilder.pathCal = ApiService.ANALYSIS_PATH.SIB_ANALYSIS_CALLLOG_PATH

        ASSycManager.init(Apps.app)
    }
}

/**
 * TODO: Data Lib 调用时机
 */
object TestDataLibInvoke {

    /**
     * TODO: 在首页每次点击申请时（在已登录的前提下），调用一下 data lib 的上报
     */
    fun sycData() {
        val token = SharedHelper.getSibSPString(SharedHelper.SIB_SPKEY.SIBLINGS_TOKEN)
        ASSycBuilder.advertId = AnalyticsUtil.getAdId()
        ASSycBuilder.androidId = AnalyticsUtil.getAndroidId()

        ASSycManager.syncData(token = token) {
            // 当前业务可忽略这个回调
        }
    }
}